package collections;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeSet;

import model.Course;
import model.CourseHistory;
import model.OfferedCourse;
import model.Registration;
import model.Student;
import persistency.controller.CourseController;
import persistency.controller.CourseHistoryController;
import persistency.controller.OfferedCourseController;
import persistency.controller.RegistrationController;
import persistency.controller.StudentController;

public class Opdracht4Bis {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		opdracht1();
		opdracht3();
		opdracht2();
	}
	/**
	 * Toon een lijst van alle cursussen met het aantal inschrijvingen (cursusnaam + aantal inschrijvingen)
	 */
	public static void opdracht1(){
		System.out.println("Opdracht 1 : Toon een lijst van alle cursussen met het aantal inschrijvingen (cursusnaam + aantal inschrijvingen)");
		Map<String,Collection<Registration>> offeredCourseRegistrations = getOfferedCourseRegistrations();
		for(Entry<String, Collection<Registration>> entry : offeredCourseRegistrations.entrySet()){
			System.out.println("\t" +  entry.getKey() + " : " + entry.getValue().size());
			
		}
	}
	
	/**
	 *  Als men het unieke cursistennummer intypt krijgt men onmiddellijk de cursushistoriek van  deze persoon (schooljaar, cursusnaam en score)
	 */
	public static void opdracht2(){
		System.out.println("Opdracht 2 : Als men het unieke cursistennummer intypt krijgt men onmiddellijk de cursushistoriek van  deze persoon (schooljaar, cursusnaam en score) ");
		System.out.println("Enter a negative number to end prompt.");
		Scanner scanner = new Scanner(System.in);
		int studentnumber = -1;
		do{
			String input = "";
			try{
				System.out.print("\tEnter studentnumber :");
				input = scanner.nextLine();
				studentnumber = Integer.parseInt(input);
				if(studentnumber > -1){
					Student student = StudentController.instance().getStudent(""+studentnumber);
					if(student != null){
						System.out.println("\t" +student.getName() + ":");
					Collection<CourseHistory> courseHistories = CourseHistoryController.instance().getByStudentIDCourseHistories(student.getStudentNumber());
					for(CourseHistory courseHistory : courseHistories){
						System.out.println("\t\t"+courseHistory);
					}
					}else{
						System.out.println("student with number " + input + " not found.");
					}
				}
				
			}catch(Exception e){
				System.out.println("failed to parse " + input + " to a number.");
			}
		
		}
		while(studentnumber > -1);
	}
	
	/**
	 * Toon een lijst van alle inschrijvingen alfabetisch geordend op cursusnaam
	 */
	public static void opdracht3(){
		System.out.println("Opdracht 3 : Toon een lijst van alle inschrijvingen alfabetisch geordend op cursusnaam");
		Map<String,Collection<Registration>> offeredCourseRegistrations = getOfferedCourseRegistrations();
		TreeSet<String> orderedOfferedCourses = new TreeSet<String>(offeredCourseRegistrations.keySet());
		 
		for(String key : orderedOfferedCourses){
			int i =1;
			System.out.println("\t" +key + ": ");
			Collection<Registration> registrations = offeredCourseRegistrations.get(key);
			for(Registration registration : registrations){
				System.out.println("\t\t" + i++ +" " +registration);
			}
		}
		
	}
	
	public static Map<String,Collection<Registration>> getOfferedCourseRegistrations(){
		Collection<OfferedCourse> offeredCourses = OfferedCourseController.instance().getAll();
		Map<String,Collection<Registration>> offeredcourseRegistrationMap = new HashMap<String,Collection<Registration>>();
		Collection<Course> courses =CourseController.instance().getAll();
		for(Course course : courses){
			offeredcourseRegistrationMap.put(course.getName(), new HashSet<Registration>());
		}
		for(OfferedCourse offeredCourse: offeredCourses){
			String courseName = offeredCourse.getCourse().getName();
			offeredcourseRegistrationMap.get(courseName).addAll(RegistrationController.instance().getByOfferedCourseIdRegistrations(offeredCourse.getId()));
		}
		return offeredcourseRegistrationMap;
	}

}
